package com.winsafe.scanlibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

public class HoneywellScanner {
    private static boolean mReceiverTag = false;
    private static Context mContext;
    public static Handler mHandler;
    private static BroadcastReceiver mBroadcastReceiver;
    private static IntentFilter mIntentFilter = new IntentFilter(HoneywellScannerInterface.ACTION_BARCODE_DATA);

    public static void init(Context context) {
        mContext = context;
    }

    /**
     * 入库管理-两个模块扫描事件接收者 修改
     * @param handler
     */
    public static void resetHandler(Handler handler) {
        HoneywellScanResultReceiver honeywellScanResultReceiver= (HoneywellScanResultReceiver) mBroadcastReceiver;
        honeywellScanResultReceiver.reset(handler);
    }

    public static void registerBroadcastReceiver() {
        if (!mReceiverTag) {
            mReceiverTag = true;
            mBroadcastReceiver = new HoneywellScanResultReceiver(mHandler);
            mContext.registerReceiver(mBroadcastReceiver, mIntentFilter);
        }
    }

    public static void unregisterBroadcastReceiver() {
        if (mReceiverTag) {   //判断广播是否注册
            mReceiverTag = false;
            mContext.unregisterReceiver(mBroadcastReceiver);
        }
    }

    public static void startScanner() {
        Bundle properties = new Bundle();
        properties.putBoolean("DPR_DATA_INTENT", true);
        properties.putString("DPR_DATA_INTENT_ACTION", HoneywellScannerInterface.ACTION_BARCODE_DATA);

        Intent intent = new Intent(HoneywellScannerInterface.ACTION_CLAIM_SCANNER);
        intent.putExtra(HoneywellScannerInterface.EXTRA_SCANNER, "dcs.scanner.imager");
//		intent.putExtra(HoneywellScannerInterface.EXTRA_PROFILE, "Default");
//		intent.putExtra(HoneywellScannerInterface.EXTRA_PROFILE, "MyProfile1");
        intent.putExtra(HoneywellScannerInterface.EXTRA_PROFILE, "Winsafe");
        intent.putExtra(HoneywellScannerInterface.EXTRA_PROPERTIES, properties);
        mContext.sendBroadcast(intent);
    }

    public static void stopScanner() {
        mContext.sendBroadcast(new Intent(HoneywellScannerInterface.ACTION_RELEASE_SCANNER));
    }
}
